<?php
/**
 * Created by PhpStorm.
 * User: woody
 * Date: 17-2-16
 * Time: 下午10:46
 */

namespace App\Http\Controllers\Admin;

use App\Models\Imgcate;
use App\Models\ImgcateImg;
use Illuminate\Http\Request;

class ImgcateController extends AdminController {

    public function __construct()
    {
        parent::__construct();
        $this->_response['_active']['_model']       = 'imgcate';
        $this->_response['_active']['_action']      = '';
        $this->_response['_title']                  = '小一农货-图片库管理';
    }

    //类型列表
    public function index(Request $request){
        $search=$request->all();
        $page = $request->get('page', 1);
        $this->_response['_active']['_action']      = 'list';

        $imgcateModel = new Imgcate();
        $data = $imgcateModel->getData($search, $page, 20);

        $this->_response['imgcate'] = $data;
        $this->_response['imgcate']['pageData']['page'] = $page;
        $this->_response['imgcate']['pageData']['pageHtml'] = self::getPageHtml($page , $this->_response['imgcate']['pageData']['lastPage'] , '/admin/imgcate?');
       // return $this->_response['imgcate'];
        return view('admin.imgcate.list', $this->_response);
    }
//添加
    public function add(){
        $this->_response['_active']['_action']      = 'add';
        return view('admin.imgcate.add', $this->_response);
    }

    public function doAdd(\App\Http\Requests\Imgcate $request){
        $data = $request->all();

        $saveData = [
            'type' => $data['type'],
            'created_at' => date('Y-m-d H:i:s', time())
        ];

        $imgcateModel = new Imgcate();
        if($imgcateModel->saveData($saveData)){
            return response()->json(['code' => 0, 'msg' => '添加成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '添加失败']);
        }
    }

    //删除
    public function delete($id){
        $Del =[
            'deleted_at' => date('Y-m-d H:i:s', time())
        ];
        $imgcateModel = new Imgcate();
        if($imgcateModel->Del($Del,$id)){
            return response()->json(['code' => 0, 'msg' => '删除成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '删除失败']);
        }
    }

    //图库列表
    public function imglist(Request $request,$id){
        $search=$request->all();
        $page = $request->get('page', 1);
        $this->_response['_active']['_action']      = 'imglist';

        $imgcateModel = new Imgcate();
        $data = $imgcateModel->getimglist($search, $page, 2,$id);
        $this->_response['cate'] = Imgcate::where('id', $id)->first();
        $this->_response['img'] = $data;
        $this->_response['img']['pageData']['page'] = $page;
        $this->_response['img']['pageData']['pageHtml'] = self::getPageHtml($page , $this->_response['img']['pageData']['lastPage'] , '/admin/imgcate/imglist/'.$id.'?');

        return view('admin.imgcate.imglist', $this->_response);
    }

    //图片添加页面
    public function imgadd($id)
    {
        $this->_response['_active']['_action'] = 'imgadd';
        $this->_response['imgcate'] = Imgcate::where('id', $id)->first();
        return view('admin.imgcate.imgadd', $this->_response);
    }

    public function imgdoAdd(\App\Http\Requests\Img $request)
    {
        $data = $request->all();
        foreach ($data['img'] as $a) {
            $imgsadd = [
                'title' => $data['title'],
                'type_id' => $data['type_id'],
                'img' => $a['imgUrl'],
                'created_at' => date('Y-m-d H:i:s', time())
            ];
            $ImgcateModel = new Imgcate();
            $info = $ImgcateModel->imgadd($imgsadd);
        }
        if (!empty($info)) {
            return response()->json(['code' => 0, 'msg' => '添加成功']);
        } else {
            return response()->json(['code' => -1, 'msg' => '添加失败']);
        }
    }

    //图片删除
    public function imgdel($id)
    {
        $del = [
            'deleted_at' => date('Y-m-d H:i:s', time())
        ];
        $ImgcateModel = new Imgcate();
        if ($ImgcateModel->imgdel($del, $id)) {
            return response()->json(['code' => 0, 'msg' => '删除成功']);
        } else {
            return response()->json(['code' => -1, 'msg' => '删除失败']);
        }
    }
}